package com.hongdee.crm.task.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.car.entity.TCrmCar;
import com.hongdee.crm.exsurance.entity.TCrmCarExsurance;
import com.hongdee.crm.task.dao.ITCrmCarWarrantyTaskDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名  称: TCrmCarExsuranceTaskDaoImpl
 * 类  型: class
 * 描  述: 车辆延保实现类
 * 路  径: com.hongdee.crm.task.dao.impl
 * 作  者: wangshuai
 * 日  期:2017/2/28 10:23
 */
@Component
public class TCrmCarWarrantyTaskDaoImpl extends BaseDaoImpl implements ITCrmCarWarrantyTaskDao {

    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> findCarExsuranceLists(String sql,int start, int limit) throws Exception {
        List<Map<String,Object>> mapList=this.getJdbcDao().queryForMapListBySqlWithParams(sql,start,limit);
        if(mapList.size()>0){
            return mapList;
        }
        return null;
    }

    /**
     * 执行批量更新续保状态信息及平均保养里程
     * @param lists
     */
    @Override
    public void updBatchCarExsucrance(List<TCrmCarExsurance> lists) {
        String sql="UPDATE t_crm_car_exsurance SET avg_mile=?,mile_status=? WHERE id=?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                TCrmCarExsurance crmCarExsurance=lists.get(i);
                ps.setString(1,crmCarExsurance.getAvgMile());
                ps.setString(2,crmCarExsurance.getMileStatus());
                ps.setString(3,crmCarExsurance.getId());
            }
            @Override
            public int getBatchSize() {
                return lists.size();
            }
        });
    }

    @Override
    public List findCarList(int start, int limit) throws Exception {
        String sqlStr="from com.hongdee.crm.car.entity.TCrmCar where mileStatus is NULL ";
        ResultEntity resultEntity=this.getHibernateDao().findPage(sqlStr,null,String.valueOf(start),String.valueOf(limit));
        if(resultEntity.getDataList().size()>0){
            return resultEntity.getDataList();
        }
        return null;
    }

    @Override
    public List findCarByMileStatusIsNull(int start, int limit) throws Exception {
        String sqlStr="from com.hongdee.crm.car.entity.TCrmCar where mileStatus is NULL ";
        ResultEntity resultEntity=this.getHibernateDao().findPage(sqlStr,null,String.valueOf(start),String.valueOf(limit));
        if(resultEntity.getDataList().size()>0){
            return resultEntity.getDataList();
        }
        return null;
    }

    @Override
    public List<Map<String,Object>> findCarByMileStatusIsNullToJdbc(int start, int limit)throws Exception{
        String sqlstr="select * from t_crm_car where mile_status is null limit ?,?";
        List<Map<String,Object>> list=this.getJdbcDao().queryForMapListBySqlWithParams(sqlstr,start,limit);
        if(list!=null){
            if(list.size()>0){
                return list;
            }
        }
        return null;
    }
    @Override
    public List findCarByMileStatus(int start,int limit) throws Exception {
        String sqlStr="from com.hongdee.crm.car.entity.TCrmCar where mileStatus < 2";
        ResultEntity resultEntity=this.getHibernateDao().findPage(sqlStr,null,String.valueOf(start),String.valueOf(limit));
        if(resultEntity.getDataList().size()>0){
            return resultEntity.getDataList();
        }
        return null;
    }

    @Override
    public void updBatchCarWarranty(List<TCrmCar> lists){
        String sql="UPDATE t_crm_car SET mile_status=? WHERE id=?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                TCrmCar crmCar=lists.get(i);
                ps.setString(1,crmCar.getMileStatus());
                ps.setString(2,crmCar.getId());
            }
            @Override
            public int getBatchSize() {
                return lists.size();
            }
        });
    }
}
